<?php

/* @TODO: TERMINAR AS REFATORAÇÕES,  */

include_once 'Answer_Patterns.php';
include_once 'Mount_DOM_tree.php';

class Refactoring_Old_Att_CSS extends Answer_Patterns {

   public function start($domDocument) {
      $result = "";
      $mount_tree = new Mount_DOM_tree();

      $allSearch = array($domDocument->getElementsByTagName('h1'), $domDocument->getElementsByTagName('h2'),
          $domDocument->getElementsByTagName('h3'), $domDocument->getElementsByTagName('h4'),
          $domDocument->getElementsByTagName('h5'), $domDocument->getElementsByTagName('h6'),
          $domDocument->getElementsByTagName('caption'), $domDocument->getElementsByTagName('iframe'),
          $domDocument->getElementsByTagName('img'), $domDocument->getElementsByTagName('input'),
          $domDocument->getElementsByTagName('legend'), $domDocument->getElementsByTagName('div'),
          $domDocument->getElementsByTagName('p'), $domDocument->getElementsByTagName('body'));

      foreach ($allSearch as $search) {
         foreach ($search as $f) {

            $style = "";
            $scriptCSS = "";
            $atributos = "";

            // "h1" || "h2" || "h3" || "h4" || "h5" || "h6" || "caption" || "iframe" || "img" || "input" || "legend" || "div" || "p" 
            if ( $f->nodeName == "h1" || $f->nodeName ==  "h2" || $f->nodeName ==  "h3" || $f->nodeName == "h4" || $f->nodeName ==  "h5" ||
                 $f->nodeName == "h6" || $f->nodeName ==  "caption" || $f->nodeName ==  "iframe" || $f->nodeName ==  "img" || $f->nodeName ==  "input" ||
                 $f->nodeName == "legend" || $f->nodeName ==  "div" || $f->nodeName ==  "p") {
               
               $style .= ($f->getAttribute("align") ? ' text-align: ' . $f->getAttribute("align") . ';' : '' );
               $atributos .= ' style="' . $style . '"';
               $removeAtt = array("align");
               $answer = '"align"';
               
            } else if ($f->nodeName == "body") {

               $scriptCSS .= ($f->getAttribute("alink") ? ' a:link { color: ' . $f->getAttribute("alink") . ' ;}' : '' );
               $scriptCSS .= ($f->getAttribute("link") ? ' a:visited { color: ' . $f->getAttribute("link") . ' ;}' : '' );
               $scriptCSS .= ($f->getAttribute("vlink") ? ' a:active { color: ' . $f->getAttribute("vlink") . ' ;}' : '' );

               $style .= ($f->getAttribute("text") ? ' color: ' . $f->getAttribute("text") . ';' : '' );
               $style .= ($f->getAttribute("background") ? ' background-image: (\'' . $f->getAttribute("background") . '\');' : '' );
               $style .= ($f->getAttribute("bgcolor") ? ' background-color: ' . $f->getAttribute("bgcolor") . ';' : '' );

               $atributos .= ' style="' . $style . '"';
               $removeAtt = array("alink", "link", "text", "vlink", "background", "bgcolor");
               $answer = '"alink", "link", "text", "vlink", "background", "bgcolor"';
            }

            if ($style != "" || $scriptCSS != "") {
               if ($scriptCSS != "") {
                  $scriptCSS = "&LT;script&GT;" . $scriptCSS . "&LT;/script&GT;<br>";
               }
               $result .= parent::elementAnswerPatterns("&LT;$f->nodeName&GT;", $f->getLineNo(), $answer, "Atributo(s) que deve(m) ser substituido(s) por CSS: ", " e contém atributos que não são mais utilizados");
               $result .= parent::tableAnswer($mount_tree->structHTML($f, 0, 0), $scriptCSS . $mount_tree->structHTMLElementRefactory($f, 0, 0, "$f->nodeName", 1, $atributos, $removeAtt));
            }
         }
      }
      if (!empty($result)) {
         $result = parent::titleAnswerPatterns("Atributos que foram substituídos por elementos CSS") . $result;
      }
      return $result;
   }

}

?>
